"use strict";

const Controller = require("egg").Controller;

class ArticleController extends Controller {
  async index() {
    const { ctx, app } = this;

    const {
      current,
      size,
      type = "",
      searchvalue = "",
      searchvolume = "",
    } = ctx.request.body;

    let sql = " ";

    if (type) {
      sql += `where type like "%${type}%" `;
    } else if (searchvalue) {
      sql += `where searchvalue like "%${searchvalue}%" `;
    } else if (searchvolume) {
      sql += `where searchvolume like "%${searchvolume}%" `;
    }

    let limit = ` limit ${(current - 1) * size} , ${size}`;

    const data = await app.mysql.query(`select * from search ` + sql + limit);

    const total = (
      await app.mysql.query(`select count(*) as count from search` + sql)
    )[0].count;

    ctx.body = {
      code: 1,
      data,
      total,
    };
  }

  async del() {
    const { ctx, app } = this;

    const { id } = ctx.request.body;

    const multipleSelection = ctx.request.body;

    if (id) {
      await app.mysql.delete("search", { id });
    }

    if (multipleSelection.length) {
      multipleSelection.forEach(async (item) => {
        await app.mysql.delete("search", { id: item.id });
      });
    }

    ctx.body = {
      code: 1,
      msg: "删除成功",
    };
  }
}

module.exports = ArticleController;
